#include "Timer.h"

namespace ds {

CTimer::CTimer() : Service("FPSTimer") {
	QueryPerformanceCounter((LARGE_INTEGER*)&lastTick);
	averageFPSCounter = 1;
	__int64 cntsPerSec = 0;
	QueryPerformanceFrequency((LARGE_INTEGER*) &cntsPerSec);
	secsPerCnt = 1.0f / (float)cntsPerSec;
	startTime = GetTickCount();
}

CTimer::~CTimer() {
}

DWORD CTimer::getRealCount() {
	return GetTickCount();//;-startTime;
}

void CTimer::update(float elapsed) {

	static float numFrames;
	static float numTime;
	__int64 currTimeStamp = 0;
	QueryPerformanceCounter((LARGE_INTEGER*) &currTimeStamp);

	float dt = (currTimeStamp - lastTick) * secsPerCnt;

    numFrames += 1.0f;
    numTime += dt;                    //cumulative time

    if( numTime >= 1.0f ) {
	   mFPS = numFrames;      
	   numTime = 0.0f;
	   numFrames = 0.0f;
	}
	lastTick = currTimeStamp;
}

float CTimer::getFPS() {
	return mFPS;
}

double CTimer::getAverageFPS() {
	return averageFPS;
}	

};

